(function(){
    if(!window.$$){
        window.$$ = {};
    }
    $$.cfg = {
        base : "/energy-webc",
        restUrl : 'http://127.0.0.1:8080/api',
        sysTitle : "系统提示！",
        gridOptSelectEmpty : "请选择操作的数据！",
        gridRemoveInfo : "您确认要删除选择的所有记录吗？"
    }
    $$.alert = function(msg){
        layer.alert(msg);
    }
    $$.msg = function(msg){
        layer.msg(msg);
    }

    $$.confirm = function(msg,after){
        layer.confirm(msg, {
            title : $$.cfg.sysTitle,
            btn: ['确认','取消'] //按钮
        }, function(index){
            layer.close(index);
            after(true);
        }, function(a,b){
            after(false);
        });
    };
    $$.goUrl = function(url){
        window.location.href = $$.cfg.base + url;
    }
    /**********************************************************
     * ajax 请求
     * service      : 服务名称
     * data         : 请求数据
     * eflag        : true,false  请求失败是否弹出alert提示，默认true
     * sflag        : true,false  请求失败是否弹出alert提示，默认false
     * seflag       : ture,false  数据错误是否弹出alert提示, 默认true
     * checkForm    : 请求前执行的函数
     * success      :  成功回调函数
     * error        : 请求失败函数
     * serror       : 请求成功，内容失败
     **********************************************************/
    
    /*$$.layout =function(){
        var $window = $(window);
        var $header = $(".main-header");
        var $sidebar = $(".main-sidebar");
        var $content = $(".content-wrapper");
        var wheight = $(document).height();
        var hheight = $header.height();
        var sheight = $sidebar.height();
        var cheight = $content.height();
        var maxHeight = wheight;
        if(sheight > maxHeight){
            maxHeight = sheight;
        }
        if(cheight > maxHeight){
            maxHeight = cheight;
        }
        if(maxHeight <= wheight){
            maxHeight = maxHeight-hheight;
        }

        $sidebar.css("min-height",maxHeight);
        $content.height(maxHeight);
    };*/

    function bindSidebarEvent(){
        $(".main-sidebar li a").click(function(e){
            //获取当前菜单
            var $this = $(this);
            //获取菜单的下一个元素
            var $menu = $this.next();
            //判定下一个元素是否子菜单
            if($menu.is('.treeview-menu')){
                if ( $menu.is(':visible')) {
                    //关闭子菜单
                    $menu.slideUp(500, function () {
                        $menu.removeClass('menu-open');
                       /* $$.layout();*/
                    });
                    //父菜单失去激状态
                    $menu.parent("li").removeClass("active");
                }else{
                    //找到父元素
                    var parent = $this.parents('ul').first();
                    //关闭所有已打开同级子菜单
                    var ul = parent.find('ul:visible').slideUp(500);
                    //删除所有已打开同级子菜单的打开样式
                    ul.removeClass('menu-open');
                    //找到子菜单
                    var parentLi = $this.parent("li");
                    //打开子菜单
                    $menu.slideDown(500, function () {
                        //添加其他子菜单打开样式
                        $menu.addClass('menu-open');
                        //删除同级子菜单的激活状态
                        parent.find('li.active').removeClass('active');
                        //激活子菜单
                        parentLi.addClass('active');
                        //调整布局
                        /*$$.layout();*/
                    });
                }
            }
        });
    }

    function initSiderbarTree($ul,menuDatas,level){
        if(menuDatas && menuDatas.length){
            for(var i = 0; i<menuDatas.length; i++){
                var menuData = menuDatas[i];
                var menuCode = menuDatas[i].menuCode;
                var status = window.menuCode == menuCode
                var  bchildren = false;
                if(menuData.children && menuData.children){
                    bchildren = true;
                }
                if(menuData.menuPath || bchildren){
                    var leafHtml = [];
                    leafHtml.push("<a  href='");
                    if(menuData.menuPath){
                        leafHtml.push(menuData.menuPath);

                        if(status){
                            leafHtml.push("'class='active'")
                        }
                    }else{
                        leafHtml.push("#'");
                    }
                    leafHtml.push(">");
                    if(menuData.menuIcon){
                        leafHtml.push("<i class='");
                        leafHtml.push(menuData.menuIcon);
                        leafHtml.push("'></i>");
                    }else{
                        if(level && !bchildren){
                            leafHtml.push("<i class='fa fa-lg fa-circle-o'></i>");
                        }else{
                            leafHtml.push("<i class='fa fa-lg fa-folder'></i>");
                        }
                    }
                    leafHtml.push("<span>");
                    leafHtml.push(menuData.menuName);
                    leafHtml.push("</span>");
                    if(menuData.menuRight){
                        leafHtml.push("<span class='label pull-right menu-label-bg'>");
                        leafHtml.push(menuData.menuRight);
                        leafHtml.push("</span>");
                    }else{
                        if(bchildren){
                            leafHtml.push("<i class='glyphicon glyphicon-chevron-left pull-right'></i>");
                        }
                    }
                    leafHtml.push("</a>");
                    var $left = $(leafHtml.join(""));
                    if(bchildren){
                        var $li = $("<li class='treeview'></li>");
                        var $cul = $("<ul class='treeview-menu'></ul>");
                        $li.append($left);
                        $li.append($cul);
                        initSiderbarTree($cul,menuData.children,level+1);
                        $ul.append($li);
                    }else{
                        var $li = $("<li ></li>") ;
                        $li.append($left);
                        $ul.append($li);
                    }
                }

            }
        }
    }

    function initSiderbarHtml(menuDatas){
        $sidebar = $(".main-sidebar");
        $sidebarUl = $(" <ul class='sidebar-menu'></ul>");
        $sidebar.append($sidebarUl);
        initSiderbarTree($sidebarUl,menuDatas,0);
        //alert($sidebar.html());
        bindSidebarEvent();
    }


    function initSiderbarData(){
        var menuDatas= [{
            "menuIcon" : "qxt-icon icon-sbgl",
            "menuName" : " 设备管理",
            "children":[
                {
                    "menuName" : "菜单A",
                    "menuPath" : "../../iot/device/collection_point_list.html",
                    "menuCode" : "device",
                }]
        },{
            "menuIcon" : "qxt-icon icon-yssj",
            "menuName" : " 源数据",
                "children":[
                    {
                        "menuName" : "菜单A",
                        "menuPath" : "/test/test.html",
                        "menuCode" : "collection"
                    },{
                        "menuName" : "菜单B",
                        "children":[
                            {
                                "menuName" : "菜单B1",
                                "menuPath" : "/test/test.html",
                            },{
                                "menuName" : "菜单B2",
                                "children":[{
                                    "menuName" : "菜单B2A",
                                    "menuPath" : "/test/test.html"
                                },{
                                    "menuName" : "菜单B2B",
                                    "menuPath" : "/test/test.html"
                                }]
                            },{
                                "menuName" : "菜单B3",
                                "menuPath" : "/test/test.html"
                            }]
                    }]
            },{
                "menuIcon" : "qxt-icon icon-sjcy",
                "menuName" : " 数据抽样",
                "children":[
                    {
                        "menuName" : "菜单A",
                        "menuPath" : "../../iot/device/collection_point_list.html",
                        "menuCode" : "device",
                    }]
            },{
                "menuIcon" : "qxt-icon icon-ysj",
                "menuName" : " 元数据",
                "children":[
                    {
                        "menuName" : "菜单A",
                        "menuPath" : "../../iot/device/collection_point_list.html",
                        "menuCode" : "device",
                    }]
            },{
                "menuIcon" : "qxt-icon icon-sjjg",
                "menuName" : " 数据加工",
                "children":[
                    {
                        "menuName" : "菜单A",
                        "menuPath" : "../../iot/device/collection_point_list.html",
                        "menuCode" : "device",
                    }]
            },{
                "menuIcon" : "qxt-icon icon-bbgl",
                "menuName" : " 报表管理",
                "children":[
                    {
                        "menuName" : "菜单A",
                        "menuPath" : "../../iot/device/collection_point_list.html",
                        "menuCode" : "device",
                    }]
            },{
            "menuIcon" : "qxt-icon icon-dsq",
            "menuName" : " 定时器管理",
            "children":[
                {
                    "menuName" : "菜单A",
                    "menuPath" : "../../iot/device/collection_point_list.html",
                    "menuCode" : "device",
                }]
            }
        ];
        initSiderbarHtml(menuDatas);
        /*$$.layout();*/
    }

    $(function(){
        initSiderbarData();
        // alert(window.menuCode)
        //注册切换事件
        $(".sidebar-toggle").on('click', function (e) {
            e.preventDefault();
            if($(window).width()>767){
                if($("#mainCont").hasClass('sidebar-collapse')){
                    $("#mainCont").removeClass('sidebar-collapse');
                    $('#userTable, #roleTable' ).bootstrapTable('resetView');
                }else{
                    $("#mainCont").addClass('sidebar-collapse');
                    $('#userTable, #roleTable' ).bootstrapTable('resetView');
                }
            }else{
                if($("#mainCont").hasClass('sidebar-open')){
                    $("#mainCont").removeClass('sidebar-open ');
                    $('#userTable, #roleTable' ).bootstrapTable('resetView');
                }else{
                    $("#mainCont").addClass('sidebar-open');
                    $('#userTable, #roleTable' ).bootstrapTable('resetView');
                }
            }

        });
        /*$(window).resize(function(){
            $$.layout();
        });

        $('.content-body').resize(function(){
            alert($(this).height());
        })
        $$.layout();*/
    });
})();
//表格重置
function tableResize(id) {
    $("#"+id).bootstrapTable('resetView');
}
